package com.leps.nq.common.excel;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.leps.nq.bu.entity.BuStation;
import com.leps.nq.bu.service.IBuStationService;
import com.leps.nq.common.util.SpringUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.function.Consumer;

@Deprecated
public class ExcelPageReadListener1<T extends ExcelImportVo> implements ReadListener<T> {
    // 导入失败的Excel数据行序号
    final List<Integer> failSerialNumbers = new ArrayList<Integer>();
    private IBuStationService stationService = SpringUtils.getBean(IBuStationService.class);
    private int totalRowCount;
    private int successRowCount;
    private int errorRowCount;
    public static int BATCH_COUNT = 100;
    private List<T> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    private final Consumer<List<T>> consumer;

    public ExcelPageReadListener1(Consumer<List<T>> consumer) {
        this.consumer = consumer;
    }

    public ExcelImportResult getResult() {
        ExcelImportResult result = new ExcelImportResult();
        result.setTotalRowCount(totalRowCount);
        result.setSuccessRowCount(successRowCount);
        result.setErrorRowCount(errorRowCount);
        result.setErrorSerialNumbers(failSerialNumbers);
        return result;
    }

    public List<Integer> getFailSerialNumbers() {
        return failSerialNumbers;
    }

    public boolean hasErrors() {
        return failSerialNumbers.size() > 0;
    }

    public int getTotalRowCount() {
        return totalRowCount;
    }

    public int getSuccessRowCount() {
        return successRowCount;
    }

    public int getErrorRowCount() {
        return errorRowCount;
    }

    @Override
    public void invoke(T data, AnalysisContext context) {
        totalRowCount++;
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            this.checkStation(cachedDataList);
            consumer.accept(cachedDataList);
            successRowCount += cachedDataList.size();
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (CollectionUtils.isNotEmpty(cachedDataList)) {
            this.checkStation(cachedDataList);
            consumer.accept(cachedDataList);
            successRowCount += cachedDataList.size();
        }
    }

    public void checkStation(List<T> list) {
        Iterator<T> iterator = list.iterator();
        while (iterator.hasNext()) {
            T vo = iterator.next();
            final Integer serialNumber = vo.getSerialNumber();
            final String stationName = StrUtil.trim(vo.getStationName());
            // 如果平台名称为空，不导入
            if (ObjectUtil.isEmpty(stationName)) {
                failSerialNumbers.add(serialNumber);
                iterator.remove();
                errorRowCount++;
                continue;
            }
            // 根据平台名称查询区块id及归属区块、归属客户
            BuStation station =  this.stationService.lambdaQuery().eq(BuStation::getStationName, stationName).one();
            // 如果查不到对应平台，不导入
            if (station == null) {
                failSerialNumbers.add(serialNumber);
                iterator.remove();
                errorRowCount++;
                continue;
            }
            // 填充数据
            vo.setStationId(station.getStationId());
            vo.setAreaId(station.getAreaId());
            vo.setAreaName(station.getAreaName());
            vo.setCustId(station.getCustId());
            vo.setCustName(station.getCustName());
        }
    }

}
